<?php

class Admin_UserController extends Australian_Controller_Action {

    // Mang tham so nhan duoc o moi Action
    protected $_arrParam;
    // Duong dan cua Controller
    protected $_currentController;
    // Duong dan cua Action chinh
    protected $_actionMain;
    protected $_namespace;
    // Tong so phan trang
    protected $_paginator = array('itemCountPerPage' => 5, 'pageRange' => 3);

    public function init() {
        // Mang tham so nhan duoc o moi Action
        $this->_arrParam = $this->_request->getParams();

        // Duong dan cua Controller
        $this->_currentController = '/' . $this->_arrParam['module']
                . '/' . $this->_arrParam['controller'];

        // Duong dan cua Action chinh
        $this->_actionMain = '/' . $this->_arrParam['module']
                . '/' . $this->_arrParam['controller'] . '/index';

        $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
        $this->_arrParam['paginator'] = $this->_paginator;

        // Luu cac du lieu filter vao SESSION
        // Dat ten SESSION
        $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
        //$ssFilter->unsetAll();

        if (empty($ssFilter->col)) {
            $ssFilter->keywords = '';
            $ssFilter->col = 'u.userid';
            $ssFilter->order = 'DESC';
            $ssFilter->group_id = 0;
        }

        $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
        $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
        $this->_arrParam['ssFilter']['order'] = $ssFilter->order;
        $this->_arrParam['ssFilter']['group_id'] = $ssFilter->group_id;

        $session = new Zend_Session_Namespace('userinfor');
        $auth = new Australian_System_Auth();
        if (!isset($session->groupid)) {
            $auth->logout();
        }

        $groupid = $session->groupid;
        $utilities = new Australian_Utilities();
        $permission = $utilities->getPermission($groupid, $this->_arrParam['controller']);
        $this->_arrParam['permission'] = $permission;
        $this->_arrParam['userinfor']['groupid'] = $groupid;
        $this->_arrParam['userinfor']['userid'] = $session->userid;
        $this->_arrParam['userinfor']['username'] = $session->username;

        // Truyen ra view
        $this->view->arrParam = $this->_arrParam;
        $this->view->currentController = $this->_currentController;
        $this->view->actionMain = $this->_actionMain;
        $this->view->countNewMessage    = $utilities->countNewMessage();

        parent::init();
        $template_path = TEMPLATE_PATH . "/admin/system";
        $this->loadTemplate($template_path, 'template.ini', 'template');
    }

    public function indexAction() {
        if (isset($this->_arrParam['permission']['view_p']) && $this->_arrParam['permission']['view_p'] == 1) {
            $this->view->Title = 'Member :: User manager :: List';
            $this->view->headTitle($this->view->Title, true);

            $tblUsers = new Admin_Model_Users();
            $this->view->Items = $tblUsers->listItem($this->_arrParam, array('task' => 'admin-list'));

            $sblGroup = new Admin_Model_Group();
            $this->view->sblGroup = $sblGroup->itemInSelectbox();

            $totalItem = $tblUsers->countItem($this->_arrParam);
            $paginator = new Australian_Paginator();
            $this->view->paginator = $paginator->createPaginator($totalItem, $this->_paginator);
        } else {
            $auth = new Australian_System_Auth();
            $auth->logout();
        }
    }

    public function filterAction() {
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
        if ($this->_arrParam['type'] == 'search') {
            if ($this->_arrParam['key'] == 1) {
                $ssFilter->keywords = trim($this->_arrParam['keywords']);
            } else {
                $ssFilter->keywords = '';
            }
        }
        if ($this->_arrParam['type'] == 'group') {
            $ssFilter->group_id = $this->_arrParam['group_id'];
        }
        if ($this->_arrParam['type'] == 'order') {
            $ssFilter->col = $this->_arrParam['col'];
            $ssFilter->order = $this->_arrParam['by'];
        }

        $this->_redirect($this->_actionMain);
        $this->_helper->viewRenderer->setNoRender();
    }

    public function addAction() {
        $action = $this->_arrParam['action'];
        if ($this->_arrParam['permission'][$action . '_p'] == 1) {
            $this->view->Title = 'Member :: User manager :: Add new';
            $this->view->headTitle($this->view->Title, true);

            $sblGroup = new Admin_Model_Group();
            $this->view->sblGroup = $sblGroup->itemInSelectbox();

            if ($this->_request->isPost()) {
                $validator = new Admin_Form_ValidateUser($this->_arrParam);
                if ($validator->isError() == true) {
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->Item = $validator->getData();
                } else {
                    $tblUser = new Admin_Model_Users();
                    $arrParam = $validator->getData(array('upload' => true));
                    $tblUser->saveItem($arrParam, array('task' => 'admin-add'));

                    $this->_redirect($this->_actionMain);
                }
            }
        } else {
            $this->view->message = Australian_Utilities::not_permission_add;
            $this->_helper->viewRenderer('index/index', null, true);
            $this->_forward('index');
        }
    }

    public function infoAction() {
        if (isset($this->_arrParam['permission']['view_p']) && $this->_arrParam['permission']['view_p'] == 1) {
            $this->view->Title = 'Member :: User manager :: Information';
            $this->view->headTitle($this->view->Title, true);

            $tblUser = new Admin_Model_Users();
            $this->view->Value = $tblUser->getItem($this->_arrParam, array('task' => 'admin-info'));
        } else {
            $auth = new Australian_System_Auth();
            $auth->logout();
        }
    }

    public function editAction() {
        $action = $this->_arrParam['action'];
        if ($this->_arrParam['permission'][$action . '_p'] == 1) {
            $this->view->Title = 'Member :: User manager :: Edit';
            $this->view->headTitle($this->view->Title, true);

            $sblGroup = new Admin_Model_Group();
            $this->view->sblGroup = $sblGroup->itemInSelectbox();

            $tblUser = new Admin_Model_Users();
            $this->view->Value = $tblUser->getItem($this->_arrParam, array('task' => 'admin-info'));

            if ($this->_request->isPost()) {
                $validator = new Admin_Form_ValidateUser($this->_arrParam);
                if ($validator->isError() == true) {
                    $this->view->messageError = $validator->getMessageError();
                    $this->view->Item = $validator->getData();
                } else {
                    $tblUser = new Admin_Model_Users();
                    $arrParam = $validator->getData(array('upload' => true));
                    $tblUser->saveItem($arrParam, array('task' => 'admin-edit'));

                    $this->_redirect($this->_actionMain);
                }
            }
        } else {
            $this->view->message = Australian_Utilities::not_permission_edit;
            $this->_helper->viewRenderer('index/index', null, true);
            $this->_forward('index');
        }
    }

    public function deleteAction() {
        $action = $this->_arrParam['action'];
        if ($this->_arrParam['permission'][$action . '_p'] == 1) {
            $this->view->Title = 'Member :: User manager :: Delete';
            $this->view->headTitle($this->view->Title, true);

            if ($this->_request->isPost()) {
                $tblUser = new Admin_Model_Users();
                $tblUser->deleteItem($this->_arrParam, array('task' => 'admin-delete'));

                $this->_redirect($this->_actionMain);
            }
        } else {
            $this->view->message = Australian_Utilities::not_permission_delete;
            $this->_helper->viewRenderer('index/index', null, true);
            $this->_forward('index');
        }
    }

    public function statusAction() {
        $action = 'edit';
        if ($this->_arrParam['permission'][$action . '_p'] == 1) {
            $tblUser = new Admin_Model_Users();
            $tblUser->changeStatua($this->_arrParam);
            $this->_redirect($this->_actionMain);
            $this->_helper->viewRenderer->setNoRender();
        } else {
            $this->view->message = Australian_Utilities::not_permission_edit;
            $this->_helper->viewRenderer('index/index', null, true);
            $this->_forward('index');
        }
    }

    public function multiDeleteAction() {
        $action = 'delete';
        if ($this->_arrParam['permission'][$action . '_p'] == 1) {
            if ($this->_request->isPost()) {
                $tblUser = new Admin_Model_Users();
                $tblUser->deleteItem($this->_arrParam, array('task' => 'admin-multi-delete'));
                $this->_redirect($this->_actionMain);
            }
            $this->_helper->viewRenderer->setNoRender();
        } else {
            $this->view->message = Australian_Utilities::not_permission_edit;
            $this->_helper->viewRenderer('index/index', null, true);
            $this->_forward('index');
        }
    }

}